<?php

namespace AppBundle\Controller\Index;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

use Symfony\Component\DependencyInjection\ContainerAware;

use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class IndexController extends Controller
{
    /**
     * construct to get webconfig
     *
     */
    public function webconfigAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $config = $em->getRepository('AppBundle:Configs')->find(1);
        
        $session = $request->getSession();
        $session->set('webconfig', $config);    
        
        return new Response();
    }

    /**
     * index page
     *
     */
    public function indexAction(Request $request)
    {
        $isMobile = isMobile();
        $template = $isMobile?'mobile':'index';

        return $this->render($template.'/index.html.twig');
    }

    /**
     * news list
     *
     */
    public function newsAction(Request $request)
    {
        # render login page
        $em = $this->getDoctrine()->getManager();

        $qb = $em->getRepository('AppBundle:Pages')->createQueryBuilder('p')->orderBy('p.createdAt', 'DESC');

        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate($qb, $request->query->getInt('page', 1),12);
        
        $count = count($pagination);

        if(isMobile()){
            $template = 'mobile';
            $pagination->setTemplate('mobile/page.html.twig');
        } else {
            $template = 'index';
            $pagination->setTemplate('index/page.html.twig');
        }

        return $this->render($template.'/news.html.twig', [
            'pagination' => $pagination,
            'count'=>$count
        ]);
    }

    /**
     * news detail
     *
     */
    public function detailAction(Request $request)
    {
        $Id = $request->get('id','');
        $em = $this->getDoctrine()->getManager();
        $page = '';
        if($Id){
            $page = $em->getRepository('AppBundle:Pages')->find($Id);
        }
        if(!$page){
            throw $this->createNotFoundException('The does not exist');
        }

        // 浏览加1
        $page->setViewsnum($page->getViewsnum()+1);
        $em->flush();

        $query = $em->getRepository('AppBundle:Pages')->createQueryBuilder('p');
        //相关
        $relations = $query->where('p.id <> '.$Id)->orderBy('p.viewsnum','DESC')->getQuery()->setMaxResults(8)->getResult();
        
        //上一篇
        $prev = $query->where('p.id < '.$Id)->orderBy('p.id','DESC')->getQuery()->setMaxResults(1)->getOneOrNullResult();
        //下一篇
        $next = $query->where('p.id > '.$Id)->orderBy('p.id','ASC')->getQuery()->setMaxResults(1)->getOneOrNullResult();

        $template = isMobile()?'mobile':'index';

        return $this->render($template.'/detail.html.twig',['page'=>$page,'relations'=>$relations,'prev'=>$prev,'next'=>$next]);
    }

    /**
     * news detail more ralation news 
     *
     */
    public function newsMoreAction(Request $request)
    {
        $pn = $request->get('pn','');
        $em = $this->getDoctrine()->getManager();
        $return['statu'] = '0';
       

        return $this->json($return);
    }
}

    